<?php
namespace app\api\controller\service;

use app\api\model\UserCustomerVisitLog;
use app\api\model\wanlshop\Auth;
use app\api\model\wanlshop\Shop;
use app\common\controller\Api;
use think\Request;

class Customer extends Api
{
    protected $noNeedRight = '*';

    protected $model;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->model = new Auth();
    }

    public function getConfig(){
        $this->success('',[
            'level' => config('site.customer_level'),
            'product_type' => config('site.customer_product_type')
        ]);
    }

    public function index(){
        extract($this->request->post());

        $mp = [];
        if(!empty($is_sea_customer) && !$is_sea_customer){
            $mp['service_id'] = $this->auth->id;
        }else{
            $mp['is_sea_customer'] = $this->auth->id;
        }

        if (!empty($search)){
            $mp["mobile|shopname|name"] = ["like","%{$search}%"];
        }

        if (!empty($is_break_contract)){
            $mp['is_break_contract'] = $is_break_contract;
        }

        if (!empty($level)){
            $mp['level'] = $level;
        }

        $data = $this->model
            ->where($mp)
            ->field('id,shopname,mt_image,province,city,district,address')
            ->order('id desc')
            ->paginate()
            ->each(function ($item){
//                if($item[''])
            });
        $this->success('客户列表',$data);
    }

    public function add(){
        $post = $this->request->post();
        $validate = $this->validate($post,'app\api\validate\service\Customer.add');

        if($validate !== true){
            $this->error($validate);
        }

        if($this->model->create($post,true)){
            $this->success('添加成功');
        }
        $this->error('添加失败');
    }

    public function edit(){
        $post = $this->request->post();
        $validate = $this->validate($post,'app\api\validate\service\Customer.edit');

        if($validate !== true){
            $this->error($validate);
        }
        $this->model = new Shop();
        if (!$this->model->where('id',$post['id'])->find()){
            $this->error('商家不存在或未通过审核');
        }
        $row = $this->model->where('id',$post['id'])->update($post);

        if($row !== false){
            $this->success('编辑成功');
        }
        $this->error('编辑失败');
    }

    public function detail(){
        $this->model = new Shop();
        if (!$row = $this->model->where('id',input('id'))->find()){
            $this->error('商家不存在');
        }
        $data = $this->model->where('user_id',$this->auth->id)->find();
        $shopInfo = Shop::get(['user_id' => $this->auth->id]);
        $shopInfo['shop_id'] = $data['id'];
        if($data['state'] == 1){

        }
    }

    public function addFollow(){
        $post = $this->request->post();
        extract($post);
        $validate = $this->validate($post,'app\api\validate\service\Customer.follow');

        if($validate !== true){
            $this->error($validate);
        }

        $this->model = new Shop();
        if (!$row = $this->model->where('id',$id)->find()){
            $this->error('商家不存在');
        }

        if($row['service_id'] != $this->auth->id){
            $this->error('不是你的客户无法跟进');
        }

        $distance = distance($lat,$lng,$row['lat'],$row['lng']);
        if($distance > 1){
            $this->error('未到达目的地一公里内无法跟进');
        }
        UserCustomerVisitLog::create([
            'service_id' => $this->auth->id,
            'shop_id' => $id,
            'content' => $content,
            'images' => $images,
            'lat' => $lat,
            'lng' => $lng,
            'address' => $address
        ]);
        $this->success('跟进成功');
    }
}